//
//  QXSingleProductViewController.swift
//  DanTangSwift
//
//  Created by 众力金融 on 2017/6/16.
//  Copyright © 2017年 ..... All rights reserved.
//

import UIKit

let collectionCellID = "QXCollectionViewCell"

class QXSingleProductViewController: QXBaseViewController {
    
    lazy var SingleProductViewModel = QXSingleProductViewModel()
    
    lazy var collectionView: UICollectionView = UICollectionView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

       self.view.backgroundColor = UIColor.white
        setupCollectionView()
        loadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension QXSingleProductViewController {
    
    func setupCollectionView() {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumLineSpacing = 10
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.itemSize = CGSize(width: (SCREWIDTH - 30) / 2, height: 245)
        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: flowLayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.QXGray
        let nib = UINib(nibName: "QXSingleProductCollectionViewCell", bundle: nil)
        collectionView.register(nib, forCellWithReuseIdentifier: collectionCellID)
        view.addSubview(collectionView)
        
    }
    
    func loadData() {
        SingleProductViewModel.singleProductData { (isSuccess) in
            print(isSuccess)
            if isSuccess {
               
                self.collectionView.reloadData()
            }else {
                
            }
        }
    }
}

extension QXSingleProductViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return SingleProductViewModel.SingleProductItems.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: collectionCellID, for: indexPath) as! QXSingleProductCollectionViewCell
        cell.backgroundColor = UIColor.white
        cell.model = SingleProductViewModel.SingleProductItems[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(10, 10, 10, 10)
    }
    
}


